package com.lsb.fitnessnutrition

import android.annotation.SuppressLint
import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.os.Build
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.navigation.Navigation
import kotlinx.android.synthetic.main.fragment_input_one_rm.*
import kotlinx.android.synthetic.main.fragment_input_one_rm.view.*
import org.tensorflow.lite.examples.classification.R

class InputOneRMFragment : Fragment() {

    @SuppressLint("Recycle")
    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_input_one_rm, container, false)
        //创建或打开数据库PowerStore.db
        val dbHelper=MyDatabaseHelper(getContext(),"PowerStore.db",null,1)
        val db: SQLiteDatabase
        db=dbHelper.writableDatabase
        val cursor: Cursor
        //查询power表，得到cursor
        cursor=db.query("power",null,null,
            null,null,null,null,null)

        view.nextStep_btn.setOnClickListener {

            if(cursor.count==0){
                //如果还没有数据，则insert数据
                //squat 1RM
                var values= ContentValues()
                var str:String
                str="squat"
                values.put("type",str)
                str=squat_one.text.toString()
                values.put("rm",str)
                db.insert("power",null,values)
                values.clear()
                //bench 1RM
                str="bench"
                values.put("type",str)
                str=bench_one.text.toString()
                values.put("rm",str)
                db.insert("power",null,values)
                values.clear()
                //lift 1RM
                str="lift"
                values.put("type",str)
                str=lift_one.text.toString()
                values.put("rm",str)
                db.insert("power",null,values)
                values.clear()
                Toast.makeText(context, "insert data succeed",
                    Toast.LENGTH_SHORT).show()
            }
            else {
                //如果有数据，则update数据
                //squat 1RM
                var values= ContentValues()
                var str:String
                str="squat"
                values.put("type",str)
                str=squat_one.text.toString()
                values.put("rm",str)
                var selectionArgs= arrayOf("squat")
                db.update("power",values,"type = ?", selectionArgs);
                values.clear()
                //bench 1RM
                str="bench"
                values.put("type",str)
                str=bench_one.text.toString()
                values.put("rm",str)
                selectionArgs= arrayOf("bench")
                db.update("power",values,"type = ?", selectionArgs);
                values.clear()
                //lift 1RM
                str="lift"
                values.put("type",str)
                str=lift_one.text.toString()
                values.put("rm",str)
                selectionArgs= arrayOf("lift")
                db.update("power",values,"type = ?", selectionArgs);
                values.clear()
                Toast.makeText(context, "update data succeed",
                    Toast.LENGTH_SHORT).show()
            }

            //新增2.5公斤取整的记录
            insertPlanTwo(db,cursor,dbHelper)

            dbHelper.createTable_plan_five(db)
            //squat train plan
            var values= ContentValues()
            //1
            var str:String
            str="squat"
            values.put("type",str)
            str=squat_one.text.toString()
            var weight = str.toInt()*0.75
            if(weight%5!=0.0){
                val max = weight + (5-weight%5)
                val min = max - 5
                weight = if((weight-min)<(max-weight)) min else max
            }
            values.put("weight",weight)
            values.put("groups","4*7")
            values.put("status","未打卡")
            db.insert("plan_five",null,values)
            values.clear()
            //2
            str="squat"
            values.put("type",str)
            str=squat_one.text.toString()
            weight = str.toInt()*0.8
            if(weight%5!=0.0){
                val max = weight + (5-weight%5)
                val min = max - 5
                weight = if((weight-min)<(max-weight)) min else max
            }
            values.put("weight",weight)
            values.put("groups","5*5")
            values.put("status","未打卡")
            db.insert("plan_five",null,values)
            values.clear()
            //3
            str="squat"
            values.put("type",str)
            str=squat_one.text.toString()
            weight = str.toInt()*0.85
            if(weight%5!=0.0){
                val max = weight + (5-weight%5)
                val min = max - 5
                weight = if((weight-min)<(max-weight)) min else max
            }
            values.put("weight",weight)
            values.put("groups","7*3")
            values.put("status","未打卡")
            db.insert("plan_five",null,values)
            values.clear()

            //bench plan
            //1
            str="bench"
            values.put("type",str)
            str=bench_one.text.toString()
            weight = str.toInt()*0.75
            if(weight%5!=0.0){
                val max = weight + (5-weight%5)
                val min = max - 5
                weight = if((weight-min)<(max-weight)) min else max
            }
            values.put("weight",weight)
            values.put("groups","4*7")
            values.put("status","未打卡")
            db.insert("plan_five",null,values)
            values.clear()
            //2
            str="bench"
            values.put("type",str)
            str=bench_one.text.toString()
            weight = str.toInt()*0.8
            if(weight%5!=0.0){
                val max = weight + (5-weight%5)
                val min = max - 5
                weight = if((weight-min)<(max-weight)) min else max
            }
            values.put("weight",weight)
            values.put("groups","5*5")
            values.put("status","未打卡")
            db.insert("plan_five",null,values)
            values.clear()
            //3
            str="bench"
            values.put("type",str)
            str=bench_one.text.toString()
            weight = str.toInt()*0.85
            if(weight%5!=0.0){
                val max = weight + (5-weight%5)
                val min = max - 5
                weight = if((weight-min)<(max-weight)) min else max
            }
            values.put("weight",weight)
            values.put("groups","7*3")
            values.put("status","未打卡")
            db.insert("plan_five",null,values)
            values.clear()

            //lift plan
            //1
            str="lift"
            values.put("type",str)
            str=lift_one.text.toString()
            weight = str.toInt()*0.75
            if(weight%5!=0.0){
                val max = weight + (5-weight%5)
                val min = max - 5
                weight = if((weight-min)<(max-weight)) min else max
            }
            values.put("weight",weight)
            values.put("groups","4*7")
            values.put("status","未打卡")
            db.insert("plan_five",null,values)
            values.clear()
            //2
            str="lift"
            values.put("type",str)
            str=lift_one.text.toString()
            weight = str.toInt()*0.8
            if(weight%5!=0.0){
                val max = weight + (5-weight%5)
                val min = max - 5
                weight = if((weight-min)<(max-weight)) min else max
            }
            values.put("weight",weight)
            values.put("groups","5*5")
            values.put("status","未打卡")
            db.insert("plan_five",null,values)
            values.clear()
            //3
            str="lift"
            values.put("type",str)
            str=lift_one.text.toString()
            weight = str.toInt()*0.85
            if(weight%5!=0.0){
                val max = weight + (5-weight%5)
                val min = max - 5
                weight = if((weight-min)<(max-weight)) min else max
            }
            values.put("weight",weight)
            values.put("groups","7*3")
            values.put("status","未打卡")
            db.insert("plan_five",null,values)
            values.clear()

            Navigation.findNavController(view).navigate(R.id.action_inputOneRMFragment_to_presetSuccessFragment)
        }


        view.threeRM_btn.setOnClickListener {
            Navigation.findNavController(view).navigate(R.id.action_inputOneRMFragment_to_inputThreeRMFragment)
        }

        view.fiveRM_btn.setOnClickListener {
            Navigation.findNavController(view).navigate(R.id.action_inputOneRMFragment_to_inputFiveRMFragment)
        }

        return view
    }

    //新增2.5公斤取整的记录
    @RequiresApi(Build.VERSION_CODES.P)
    fun insertPlanTwo(db:SQLiteDatabase, cursor: Cursor, dbHelper:MyDatabaseHelper)
    {

        dbHelper.createTable_plan_two(db)
        //squat train plan
        var values= ContentValues()
        //1
        var str:String
        str="squat"
        values.put("type",str)
        str=squat_one.text.toString()
        var weight = str.toInt()*0.75
        if(weight%2.5!=0.0){
            val max = weight + (2.5-weight%2.5)
            val min = max - 2.5
            weight = if((weight-min)<(max-weight)) min else max
        }
        values.put("weight",weight)
        values.put("groups","4*7")
        values.put("status","未打卡")
        db.insert("plan_two",null,values)
        values.clear()
        //2
        str="squat"
        values.put("type",str)
        str=squat_one.text.toString()
        weight = str.toInt()*0.8
        if(weight%2.5!=0.0){
            val max = weight + (2.5-weight%2.5)
            val min = max - 2.5
            weight = if((weight-min)<(max-weight)) min else max
        }
        values.put("weight",weight)
        values.put("groups","5*5")
        values.put("status","未打卡")
        db.insert("plan_two",null,values)
        values.clear()
        //3
        str="squat"
        values.put("type",str)
        str=squat_one.text.toString()
        weight = str.toInt()*0.85
        if(weight%2.5!=0.0){
            val max = weight + (2.5-weight%2.5)
            val min = max - 2.5
            weight = if((weight-min)<(max-weight)) min else max
        }
        values.put("weight",weight)
        values.put("groups","7*3")
        values.put("status","未打卡")
        db.insert("plan_two",null,values)
        values.clear()

        //bench plan
        //1
        str="bench"
        values.put("type",str)
        str=bench_one.text.toString()
        weight = str.toInt()*0.75
        if(weight%2.5!=0.0){
            val max = weight + (2.5-weight%2.5)
            val min = max - 2.5
            weight = if((weight-min)<(max-weight)) min else max
        }
        values.put("weight",weight)
        values.put("groups","4*7")
        values.put("status","未打卡")
        db.insert("plan_two",null,values)
        values.clear()
        //2
        str="bench"
        values.put("type",str)
        str=bench_one.text.toString()
        weight = str.toInt()*0.8
        if(weight%2.5!=0.0){
            val max = weight + (2.5-weight%2.5)
            val min = max - 2.5
            weight = if((weight-min)<(max-weight)) min else max
        }
        values.put("weight",weight)
        values.put("groups","5*5")
        values.put("status","未打卡")
        db.insert("plan_two",null,values)
        values.clear()
        //3
        str="bench"
        values.put("type",str)
        str=bench_one.text.toString()
        weight = str.toInt()*0.85
        if(weight%2.5!=0.0){
            val max = weight + (2.5-weight%2.5)
            val min = max - 2.5
            weight = if((weight-min)<(max-weight)) min else max
        }
        values.put("weight",weight)
        values.put("groups","7*3")
        values.put("status","未打卡")
        db.insert("plan_two",null,values)
        values.clear()

        //lift plan
        //1
        str="lift"
        values.put("type",str)
        str=lift_one.text.toString()
        weight = str.toInt()*0.75
        if(weight%2.5!=0.0){
            val max = weight + (2.5-weight%2.5)
            val min = max - 2.5
            weight = if((weight-min)<(max-weight)) min else max
        }
        values.put("weight",weight)
        values.put("groups","4*7")
        values.put("status","未打卡")
        db.insert("plan_two",null,values)
        values.clear()
        //2
        str="lift"
        values.put("type",str)
        str=lift_one.text.toString()
        weight = str.toInt()*0.8
        if(weight%2.5!=0.0){
            val max = weight + (2.5-weight%2.5)
            val min = max - 2.5
            weight = if((weight-min)<(max-weight)) min else max
        }
        values.put("weight",weight)
        values.put("groups","5*5")
        values.put("status","未打卡")
        db.insert("plan_two",null,values)
        values.clear()
        //3
        str="lift"
        values.put("type",str)
        str=lift_one.text.toString()
        weight = str.toInt()*0.85
        if(weight%2.5!=0.0){
            val max = weight + (2.5-weight%2.5)
            val min = max - 2.5
            weight = if((weight-min)<(max-weight)) min else max
        }
        values.put("weight",weight)
        values.put("groups","7*3")
        values.put("status","未打卡")
        db.insert("plan_two",null,values)
        values.clear()
    }

}